package lime.dql.view;

import java.util.List;

import lime.common.LimeException;
import lime.dql.provider.ResultContentProvider;
import lime.dql.provider.ResultLabelProvider;

import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

import com.documentum.fc.client.IDfTypedObject;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.IDfAttr;

public class DqlResultsView extends ViewPart {

	public static final String ID = "lime.dql.view.results";

	// Widgets
	TableViewer viewer;
	Composite parent;

	@Override
	public void createPartControl(Composite parent) {

		this.parent = parent;

		viewer = new TableViewer(parent);
		viewer.getTable().setHeaderVisible(true);
		viewer.getTable().setLinesVisible(true);

	}

	public void update(List<IDfTypedObject> typedObjects) {

		for (Control control : parent.getChildren()) {
			control.dispose();
		}

		viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.BORDER);

		final ResultComparator comparator = new ResultComparator();
		viewer.setComparator(comparator);
		
		try {
			IDfTypedObject firstObject = typedObjects.get(0);
			for (int i = 0; i < firstObject.getAttrCount(); i++) {
				final int index = i;
				IDfAttr attr = firstObject.getAttr(i);
				
				final TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
				column.getColumn().setText(attr.getName());
				column.getColumn().addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						comparator.setColumn(index);
						int dir = comparator.getDirection();
						viewer.getTable().setSortDirection(dir);
						viewer.getTable().setSortColumn(column.getColumn());
						viewer.refresh();
					}
				});
				
			}
		} catch (DfException e) {
			throw new LimeException(e);
		}

		viewer.getTable().setLayout(new TableLayout());
		viewer.getTable().setLinesVisible(true);
		viewer.getTable().setHeaderVisible(true);

		viewer.setLabelProvider(new ResultLabelProvider());
		viewer.setContentProvider(new ResultContentProvider());
		viewer.setInput(typedObjects);

		// Make sure the columns take up as much space as needed, but not more.
		for (TableColumn column : viewer.getTable().getColumns()) {
			column.pack();
		}

		parent.layout();
	}

	public void clearResults() {
		for (Control control : parent.getChildren()) {
			control.dispose();
		}
		
		viewer = new TableViewer(parent);
		viewer.getTable().setHeaderVisible(true);
		viewer.getTable().setLinesVisible(true);
	}

	@Override
	public void setFocus() {
		viewer.getTable().setFocus();
	}

	public TableViewer getViewer() {
		return viewer;
	}
}
